package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.QuotationMapper;
import com.ruoyi.crm.domain.Quotation;
import com.ruoyi.crm.service.IQuotationService;

/**
 * 报价单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-27
 */
@Service
public class QuotationServiceImpl implements IQuotationService
{
    @Autowired
    private QuotationMapper quotationMapper;

    /**
     * 查询报价单
     *
     * @param id 报价单主键
     * @return 报价单
     */
    @Override
    public Quotation selectQuotationById(String id)
    {
        return quotationMapper.selectQuotationById(id);
    }

    /**
     * 查询报价单列表
     *
     * @param quotation 报价单
     * @return 报价单
     */
    @Override
    public List<Quotation> selectQuotationList(Quotation quotation)
    {
        return quotationMapper.selectQuotationList(quotation);
    }

    /**
     * 新增报价单
     *
     * @param quotation 报价单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuotation(Quotation quotation)
    {
        int rows = quotationMapper.insertQuotation(quotation);
        insertCustomerProduct(quotation);
        return rows;
    }

    /**
     * 修改报价单
     *
     * @param quotation 报价单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuotation(Quotation quotation)
    {
        quotation.setUpdateTime(DateUtils.getNowDate());
        quotationMapper.deleteCustomerProductByCustomerName(quotation.getId());
        insertCustomerProduct(quotation);
        return quotationMapper.updateQuotation(quotation);
    }

    /**
     * 批量删除报价单
     *
     * @param ids 需要删除的报价单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuotationByIds(String[] ids)
    {
        return quotationMapper.deleteQuotationByIds(ids);
    }

    /**
     * 删除报价单信息
     *
     * @param id 报价单主键
     * @return 结果
     */
    @Override
    public int deleteQuotationById(String id)
    {
        quotationMapper.deleteCustomerProductByCustomerName(id);
        return quotationMapper.deleteQuotationById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param quotation 报价单对象
     */
    public void insertCustomerProduct(Quotation quotation)
    {
        List<CustomerProduct> customerProductList = quotation.getCustomerProductList();
        String id = quotation.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                quotationMapper.batchCustomerProduct(list);
            }
        }
    }
}